import { useTable, LIST_TYPE } from '@crud/index';
import { getUserList as getList } from '@/api/crm-kgj/user';
import { formatParamsSelectVal } from '@/utils';
import { useRoute } from 'vue-router';

/** 平台枚举 */
export const platformOptions = [
  { label: '全部', value: 0 },
  { label: 'pc', value: 1 },
  { label: '品牌词', value: 2 },
  { label: '小程序', value: 3 },
  { label: '安卓', value: 4 },
  { label: '苹果', value: 5 }
];
/** 会员类型枚举 */
export const levelOptions = [
  { label: '至尊版', value: 30 },
  { label: '旗舰版', value: 20 },
  { label: '轻享版', value: 10 },
  { label: '体验版', value: 0 }
];
/** 预设备注 */
export const wordList = ['微信小店', '抖店', '淘宝', '对公转账', '加法公社', '智趣', '考古加'];
/** 快速日期枚举 */
export const quickDateMap = [
  {
    label: '3天',
    value: [3, 'd']
  },
  {
    label: '5天',
    value: [5, 'd']
  },
  {
    label: '7天',
    value: [7, 'd']
  },
  {
    label: '10天',
    value: [10, 'd']
  },
  {
    label: '15天',
    value: [15, 'd']
  },
  {
    label: '一月',
    value: [1, 'month']
  },
  {
    label: '一季',
    value: [3, 'month']
  },
  {
    label: '半年',
    value: [6, 'month']
  },
  {
    label: '一年',
    value: [1, 'year']
  }
];
/** 会员时长枚举 */
export const durationOptions = [
  { label: '1天', value: 1 },
  { label: '3天', value: 2 },
  { label: '5天', value: 3 },
  { label: '7天', value: 4 },
  { label: '10天', value: 5 },
  { label: '15天', value: 6 },
  { label: '一月', value: 7 }
];
/** 有效期枚举 */
export const validDateOptions = [
  { label: '3天', value: 1 },
  { label: '7天', value: 2 },
  { label: '14天', value: 3 },
  { label: '30天', value: 4 },
  { label: '永久', value: 5 }
];
export const useTableList = (options: any = {}) => {
  const route = useRoute();
  const columns = [
    {
      label: '用户ID',
      prop: 'user_id',
      align: 'center',
      fixed: 'left',
      width: 100
    },
    {
      label: '用户昵称',
      prop: 'nick_name',
      render: 'nick_name',
      width: 296
    },
    {
      label: '手机号',
      prop: 'mobile',
      align: 'center',
      width: 122
    },
    {
      label: '注册时间',
      prop: 'add_time',
      type: 'dateTime',
      align: 'center',
      width: 177
    },
    {
      label: '会员类型',
      prop: 'level_name',
      align: 'center',
      width: 100
    },
    {
      label: '团队人数',
      prop: 'slave_count',
      align: 'center',
      width: 100
    },
    {
      label: '到期时间',
      prop: 'end_time',
      type: 'dateTime',
      align: 'center',
      width: 177
    },
    {
      label: '最后活跃时间',
      prop: 'last_login_time',
      type: 'dateTime',
      align: 'center',
      width: 177
    },
    {
      label: '平台',
      prop: 'platform',
      align: 'center',
      width: 77
    },
    {
      label: '1级渠道',
      prop: 'utm_source',
      align: 'center',
      showOverflowTooltip: true,
      width: 100
    },
    {
      label: '2级渠道',
      prop: 'utm_term',
      align: 'center',
      showOverflowTooltip: true,
      width: 100
    },
    {
      label: '封号',
      prop: 'is_frozen',
      align: 'center',
      render: (s) => (s.row.is_frozen ? '是' : '否'),
      width: 77
    },
    {
      label: '注销',
      prop: 'is_abandon',
      align: 'center',
      render: (s) => (s.row.is_abandon ? '是' : '否'),
      width: 77
    },
    {
      label: '白名单',
      prop: 'is_bitwise',
      align: 'center',
      render: (s) => (s.row.is_bitwise ? '是' : '否'),
      width: 77
    },
    {
      label: '绑定讲师',
      prop: 'lecturer',
      align: 'center',
      showOverflowTooltip: true,
      width: 120
    },
    {
      label: '备注',
      prop: 'remark',
      align: 'center',
      showOverflowTooltip: true,
      minWidth: 200
    },
    {
      label: '操作',
      align: 'center',
      render: 'actions',
      width: 160,
      fixed: 'right'
    }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.KGJ,
    params: {
      keyword: route.query.user_id ?? '',
      agent_id: -1,
      wd: 0
    },
    getParams: ({ _timeArr = [], ...params }) => {
      // 处理时间参数
      if (_timeArr?.length) {
        const [begin, end] = _timeArr;
        params.begin = begin;
        params.end = end;
      }
      return {
        ...params,
        ...formatParamsSelectVal(
          [
            ['agent_id', -1],
            ['wd', 0]
          ],
          params
        )
      };
    },
    columns,
    getList
  });
};
/** 日志列表 */
export const useLogTableList = (options) => {
  const columns = [
    {
      label: '请求时间',
      prop: 'request_time',
      align: 'center',
      type: 'dateTime',
      fixed: 'left',
      width: 177
    },
    {
      label: '路径',
      prop: 'path',
      align: 'center',
      showOverflowTooltip: true,
      minWidth: 258
    },
    {
      label: '数据包',
      prop: 'request_body',
      align: 'center',
      type: 'copy',
      showOverflowTooltip: true,
      width: 150
    },
    {
      label: '平台',
      prop: 'platform',
      align: 'center',
      width: 80
    },
    {
      label: '渠道',
      prop: 'channel',
      align: 'center',
      width: 100
    },
    {
      label: '设备类型',
      prop: 'device_type',
      align: 'center',
      width: 100
    },
    {
      label: '接口版本',
      prop: 'api_version',
      align: 'center',
      width: 80
    },
    {
      label: 'ip地址',
      prop: 'request_ip',
      align: 'center',
      width: 140
    }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.KGJ,
    columns,
    getListOnInit: false,
    usePagination: false
  });
};
